package com.example.fp.cgc.controller

import com.example.fp.cgc.entity.Apps
import com.example.fp.cgc.entity.PageCondition
import com.example.fp.cgc.mapper.AppsMapper
import com.example.fp.cgc.util.PageUtil
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*
import org.springframework.web.servlet.ModelAndView


/**
 * @author 蔡
 * @since 2020-01-03
 */
@Controller
@RequestMapping
open class AppWebController {

    @Autowired
    @Lazy
    lateinit var appsMapper: AppsMapper

    /**
     * 获取应用列表
     * @author 蔡
     */
    @RequestMapping(value = ["/appsList"])
    open fun getAppsListWeb(model: Model): ModelAndView {
        val appsList = appsMapper.selectAppsList()

        model.addAttribute("appsList", appsList)
        return ModelAndView("appslist/appslist", "appsModel", model)
    }

    var search = ""

    /**
     * @author 蔡
     * @since 2020-01-19
     */
    @GetMapping("/getAppsByName/{page}")
    open fun getAppsByName(
            @PathVariable("page") page: Int,
            @RequestParam(required = false, defaultValue = "") titlename: String,
            model: Model): ModelAndView {
        val count = if (titlename.isNotEmpty() && titlename != "null") {
            model.addAttribute("titlename", titlename)
            search = titlename
            appsMapper.getAppsByName(titlename)
        } else {
            appsMapper.selectAppsCount()
        }

        if (count == 0) {
            model.addAttribute("count", count)
            return ModelAndView("appslist/appslist", "appsModel", model)
        } else {
            val pageSize = 10

            val pageData = PageUtil.getPage(page, count, pageSize)

            if (titlename.isNotEmpty() && titlename != "null") {
                val pc = PageCondition(pageData.currLoc, pageSize, titlename)
                val appsList: List<Apps> = appsMapper.selectSearchAppsPage(pc)
                model.addAttribute("appsList", appsList)
            } else {
                val pc = PageCondition(pageData.currLoc, pageSize, "")
                val appsList: List<Apps> = appsMapper.selectAppsPage(pc)
                model.addAttribute("appsList", appsList)
            }

            model.addAttribute("pageNo", pageData.page)
            model.addAttribute("count", count)
            model.addAttribute("maxpage", pageData.maxPage)
            model.addAttribute("currMaxPage", pageData.currMaxPage)

            return ModelAndView("appslist/appslist", "appsModel", model)
        }
    }

    @RequestMapping(value = ["/success"])
    open fun success(model: Model): ModelAndView {
        return ModelAndView("appslist/success", "appsModel", model)
    }

    /**
     * 获取创建表单页面
     * @author: 蔡
     * @param model
     * @return
     * @since 2020-01-04
     */
    @GetMapping("/appsForm")
    open fun createAppForm(model: Model): ModelAndView? {
        model.addAttribute("apps", Apps())
        model.addAttribute("title", "创建用户")
        return ModelAndView("appslist/appsAdd", "appsModel", model)
    }

    /**
     * 保存或者更新应用，并返回应用列表
     * @author: 蔡
     * @param app
     * @return
     * @since 2020-01-04
     */
    @PostMapping("/addOrModityApps")
    open fun saveOrUpdateApp(
            apps: Apps
    ): ModelAndView {
        val app1 = appsMapper.selectById(apps.id)
        if (app1 == null) {
            appsMapper.insert(apps)
            return ModelAndView("redirect:/success")
        } else {
            val titlename = search
            val index = if (search.isNotEmpty() && search != "null") {
                appsMapper.getSearchAppIndex(apps.id, search)
            } else {
                appsMapper.getAppIndex(apps.id)
            }
            val i = index % 10
            var page = index / 10
            if (i > 0) {
                page += 1
            }

            appsMapper.updateById(apps)
            return ModelAndView("redirect:/getAppsByName/${page}/?titlename=$titlename") //重定向到list页面
        }
    }

    /**
     * 删除应用，并返回应用列表
     * @author: 蔡
     * @param id
     * @return
     * @since 2020-01-04
     */
    @GetMapping("/deleteApps/{id}&{page}&{titlename}")
    open fun deleteApps(
            @PathVariable("id") id: Int,
            @PathVariable("page") page: Int,
            @RequestParam(required = false, defaultValue = "") titlename: String
    ): ModelAndView {
        appsMapper.deleteById(id)
        return ModelAndView("redirect:/getAppsByName/${page}/?titlename=$titlename") //重定向到list页面
    }

    /**
     * 批量删除
     * @author 蔡
     * @since 2010-01-05
     */
    @GetMapping("/delSelectedApps/{checkedID}&{page}&{titlename}")
    open fun delSelected(
            @PathVariable("checkedID") checkedID: String,
            @PathVariable("page") page: Int,
            @PathVariable("titlename") titlename: String
    ): ModelAndView {
        val ids = (checkedID as java.lang.String).split(",")
        for (i in ids.indices) {
            appsMapper.deleteById(ids[i].toInt())
        }
        return ModelAndView("redirect:/getAppsByName/${page}/?titlename=$titlename") //重定向到list页面
    }

    /**
     * 修改用户跳转页面
     * @author: 蔡
     * @param model
     * @return
     * @since 2020-01-04
     */
    @GetMapping("/modifyApps/{id}&{titlename}")
    open fun modifyApp(
            @PathVariable("id") id: Int,
            @PathVariable("titlename") titlename: String,
            model: Model
    ): ModelAndView {
        search = titlename
        model.addAttribute("apps", appsMapper.selectById(id))
        model.addAttribute("title", "修改应用")
        model.addAttribute("titlename", titlename)
        return ModelAndView("appslist/modifyApps", "appsModel", model)
    }
}